// +----------------------------------------------------------------------
// | CmPage [ 通用页面框架.手机端 ]
// +----------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0
// +----------------------------------------------------------------------
// | Author: defans <defans@sina.cn>
// +----------------------------------------------------------------------

/**
  cmpage的公共属性和方法
 */

var cmpage={
	modulename:'',	//模块名称
	parmsUrl:{},	//URL中的参数对象
	curID:0,	//当前记录ID
	parentView:null,	//当前窗口的父窗口
	gps:{			//经纬度
		lat:0,
		lng:0
	},

	//手机端加载的窗体页面
	html:{
		LIST:'/html/cmpage/cmpage-list.html',	//列表页
		LIST_POP:'/html/cmpage/cmpage-list-pop.html',	//弹出列表页
		SEARCH:'/html/cmpage/cmpage-search.html',	//搜索条件页
		EDIT:'/html/cmpage/cmpage-edit.html',		//编辑页面
		VIEW:'/html/cmpage/cmpage-view.html',		//查看页面
		LIST_FILE:'/html/cmpage/cmpage-list_file.html',	//附件列表页
		FILE_PICKER:'/html/cmpage/cmpage-filepicker.html',	//文件、照片页
		SCAN:'/html/cmpage/cmpage-scan.html',	//二维码扫描页
		UPLOADER:'/html/cmpage/cmpage-uploader.html'	//附件上传-

	},

	//初始化
	init = function(){
		cmpage.modulename = mui.currentWebview.modulename;
	    cmpage.parmsUrl = mui.currentWebview.parmsUrl;
		cmpage.curID = mui.currentWebview.curID || 0;
		cmpage.parentView = plus.webview.currentWebview().opener();
		app.getPosBaidu(function(lat,lng){
			cmpage.gps.lat = lat;
			cmpage.gps.lng = lng;
		});
	},

	//根据后端传回的样式类名，增加相应的行为
	//此处为一般的行为逻辑，如果需要特殊处理，则后端传回特定样式类名，然后额外增加事件监听
	addListener = function(){
		//打开弹出窗口的按钮
		mui('.cmpage-btn-view').on('tap', 'a', function() {
			var url ='';
			switch (this.getAttribute('data-type')) {
				case 'cmpage-edit':			url = cmpage.html.EDIT;			break;
				case 'cmpage-view':			url = cmpage.html.VIEW;			break;
				case 'cmpage-list-file':	url = cmpage.html.LIST_FILE;	break;
				case 'cmpage-list-pop':		url = cmpage.html.LIST_POP;		break;
				case 'cmpage-scan':			url = cmpage.html.SCAN;			break;
				case 'cmpage-uploader':		url = cmpage.html.UPLOADER;		break;
				default:	url = '';
			};

			var	extras = {
					modulename: this.getAttribute('data-modulename') || cmpage.modulename,
					curID: this.getAttribute('data-id') || cmpage.curID,
					parms: eval('(' + this.getAttribute('data-parms') + ')') || {},
					parmsUrl: eval('(' + this.getAttribute('data-parmsUrl') + ')') || {}
				};
			}
			//console.log(JSON.stringify(extras));
			setTimeout(function() {
				mui.openWindow({
					id: url,
					url: url,
					extras: extras,
					show: {
						autoShow: false
					}
				});
			}, 100);
		});

		//执行某个动作的按钮
		mui('.cmpage-btn-action').on('tap', 'a', function() {
			var elem = this;
			plus.nativeUI.confirm("确定要" + this.innerHTML + "吗?", function(e) {
				if (e.index == 0) {
					mui.ajax(app.getDomain() + url, {
						data: parm,
						type: "post",
						dataType: 'json',
						timeout: 5000,
						error: function(request) {
							app.toast("服务器通信失败！");
						},
						success: function(data) {
							app.toast(data.message);
							if(typeof pulldownRefresh === 'function')	pulldownRefresh();
							//						if(elem.innerHTML == "删除") {
							//							elem.parentNode.parentNode.parentNode.removeChild(elem.parentNode.parentNode);
							//						}
						}
					});
				}
			}, this.innerHTML, ["确定", "取消"]);
		});

		//地区3级联动
		mui('.cmpage-picker-country').on('tap', 'a', function() {
			var picker = new mui.PopPicker({
				layer: 3
			});
			picker.setData(cityData3);		//html文件中需要先加载 <script src="../../mui/js/city.data-3.js" type="text/javascript" charset="utf-8"></script>
			var ref = citypickerbt.getAttribute('data-ref');
			picker.show(function(items) {
				btn.innerHTML = (items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text;
				document.getElementById(ref).value = (items[0] || {}).value + "," + (items[1] || {}).value + "," + (items[2] || {}).value;
				//picker.dispose();
			});
		});

		//日期时间选择
		mui('.cmpage-picker-datetime').on('tap', 'a', function() {
			var options = JSON.parse(btn.getAttribute('data-options') || '{}');
			var picker = new mui.DtPicker(options);
			var ref = btn.getAttribute('data-ref');
			picker.setSelectedValue(document.getElementById(ref).value);
			picker.show(function(rs) {
				document.getElementById(ref).value = rs.text;
				btn.innerHTML = rs.text;
				//picker.dispose();
			});
		});

		//查找带回的按钮
		mui('.cmpage-btn-lookup').on('tap', 'a', function() {
			var parms = JSON.parse(this.getAttribute('data-parms') || '{}');
			setTimeout(function() {
				mui.openWindow({
					id: cmpage.html.LIST_POP,
					url: cmpage.html.LIST_POP,
					extras: {
						parms:parms,
						parmsUrl:this.getAttribute('data-parmsUrl') || '{}',
						modulename:parms.modulename,
						title:'请选择'
					}
				});
			}, 100);
		});

		//查找带回的结果返回
		mui('.cmpage-lookup-back').on('tap', 'a', function() {
			var view = plus.webview.currentWebview().opener();
			mui.fire(view, "cmpage_edit_lookup_result", {
				result: eval('(' + this.getAttribute('data-parm') + ')') || {};
			});
			mui.back();
		});

	}
};
